﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Windows;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;
using PrsmMvvm.Models;

namespace PrsmMvvm.ViewModels
{
    public class FacilityWindowViewModel : BindableBase
    {
        public DelegateCommand<Window> btn_Close { get; set; }
        public DelegateCommand btn_Last { get; set; }
        public DelegateCommand btn_Next { get; set; }

        //数据源
        ObservableCollection<Facility> _mylist = new ObservableCollection<Facility>();
        public ObservableCollection<Facility> mylist
        {
            get { return _mylist; }
            set
            {
                _mylist = value;
                RaisePropertyChanged("mylist");
            }
        }

        #region 图表类型
        private SeriesCollection _SeriesCollection;
        public SeriesCollection SeriesCollection
        {
            get { return _SeriesCollection; }
            set
            {
                if (_SeriesCollection != value)
                {
                    _SeriesCollection = value;
                    RaisePropertyChanged("SeriesCollection");
                }
            }
        }

        private SeriesCollection _SeriesCollectionp;
        public SeriesCollection SeriesCollectionp
        {
            get { return _SeriesCollectionp; }
            set
            {
                if (_SeriesCollectionp != value)
                {
                    _SeriesCollectionp = value;
                    RaisePropertyChanged("SeriesCollectionp");
                }
            }
        }

        private string[] _Labels;
        public string[] Labels
        {
            get { return _Labels; }
            set
            {
                if (_Labels != value)
                {
                    _Labels = value;
                    RaisePropertyChanged("Labels");
                }
            }
        }
        #endregion 

        //构造函数
        public FacilityWindowViewModel()
        {
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });
            mylist.Add(new Facility() { uID = 1, uDM = "张三", uGZ = "无", uXX = "无", uFSSJ = "无", uZT = "无", uJY = "无", uYC = "无" });

            ChartStart();
            Piestart();

            btn_Close = new DelegateCommand<Window>(btnClose);
            btn_Last = new DelegateCommand(btnLast);
            btn_Next = new DelegateCommand(btnNext);
        }

        public void Piestart()
        {
            SeriesCollectionp = new SeriesCollection
            {

            new PieSeries
                            {
                                Title = "Chrome",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
            new PieSeries
                            {
                                Title = "Mozilla",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
             new PieSeries
                            {
                                Title = "Opera",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
             new PieSeries
                            {
                                Title = "Explorer",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            }
             };
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        var r = new Random();
                        var c = SeriesCollectionp.Count > 0 ? SeriesCollectionp[0].Values.Count : 5;

                        var vals = new ChartValues<ObservableValue>();
                        for (var i = 0; i < c; i++)
                        {
                            vals.Add(new ObservableValue(r.Next(1, 10)));
                        }
                        SeriesCollectionp.Add(new PieSeries
                        {
                            Title = r.ToString(),
                            Values = vals,
                            DataLabels = true
                        });
                        //更新横坐标
                        SeriesCollectionp.RemoveAt(0);
                    });
                }
            });
        }

        private void btnClose(Window window)
        {
            if (window != null)
            {
                window.Close();
            }
        }

        private void btnLast()
        {
            Views.SeWindow window = new Views.SeWindow();
            window.Show();

            if (window != null)
            {
                window.Close();
            }
        }

        private void btnNext()
        {
            Views.ProduceWindow window = new Views.ProduceWindow();
            window.Show();
        }

        public void ChartStart()
        {
            double _trend;
            double _trend1;

            SeriesCollection = new SeriesCollection
            {
            new ColumnSeries
                {
                    Title = "2015",
                    Values = new ChartValues<double> { 10, 50, 39, 50 }
                },
            new ColumnSeries
                {
                  Title = "2016",
                Values = new ChartValues<double> { 11, 56, 42 , 48 }
                }
             };

            Labels = new[] { "手工", "自动" };

            Func<double, string> Formatter = value => value.ToString("N");

            Task.Run(() =>
            {
                var r = new Random();
                while (true)
                {
                    Thread.Sleep(1000);
                    _trend = r.Next(0, 100);
                    _trend1 = r.Next(0, 100);

                    //通过Dispatcher在工作线程中更新窗体的UI元素
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        //更新横坐标
                        SeriesCollection[0].Values.Add(_trend);
                        SeriesCollection[1].Values.Add(_trend1);
                        SeriesCollection[0].Values.RemoveAt(0);
                        SeriesCollection[1].Values.RemoveAt(0);
                    });
                }
            });
        }


    }

}
